package com.hllxd.hism.star.basic.service.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/12/25.
 */
@Slf4j
public class Utils {

//    public static String getReverseInteger(int num) {
//        StringBuilder str = new StringBuilder();
//        str.append(num);
//        final String numStr = str.reverse().toString();
//        if (numStr.length() < 8) {
//            String blankStr = "";
//            for (int i=0;i<8 - numStr.length();i++) {
//                blankStr += "0";
//            }
//            return (numStr + blankStr);
//        } else {
//            return numStr.substring(0, 8);
//        }
//    }
    public static String getTraceID() {
        ConsoleContext consoleContext = ThreadLocalManager.getConsoleContext();
        if(consoleContext==null) {
            return null;
        }
        return consoleContext.getTraceId();
    }
    public static Long toLong(String v) {
        if(StringUtils.isEmpty(v)) {
            return 0L;
        }
        try {
            return Long.valueOf(v);
        }catch (Exception e) {
            return 0L;
        }
    }


    public static void main1(String[] args){
//        String s = "1.111";
//        System.out.println(String.format("%.1f",Double.parseDouble(s)));

        String json = "{\"status\":\"SUCCESS\", \"checkTime\":20200116203001, \"totalSummary\": {\"score\":90.0, \"total\": 15, \"abnormal\": 3},\n"
            + "    \"businessResult\":{\"summary\": {\"score\":90.0, \"total\": 3, \"abnormal\": 1}, \"result\":[\n"
            + "        {\"content\":\"单均消费1\", \"result\":\"0异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费2\", \"result\":\"0异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费3\", \"result\":\"0异常\",\"score\":20,\"level\":1}\n" + "        ]},\n"
            + "    \"diffreconResult\":{\"summary\": {\"score\":90.0, \"total\": 2, \"abnormal\": 0}, \"result\":[\n"
            + "        {\"content\":\"单均消费4\", \"result\":\"0异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费5\", \"result\":\"0异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费6\", \"result\":\"0异常\",\"score\":20,\"level\":1}\n" + "        ]},\n"
            + "    \"foodSaleResult\":{\"summary\": {\"score\":90.0, \"total\": 2, \"abnormal\": 1}, \"result\":[\n"
            + "        {\"content\":\"单均消费7\", \"result\":\"0异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费8\", \"result\":\"0异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费9\", \"result\":\"0异常\",\"score\":20,\"level\":1}\n" + "        ]},\n"
            + "    \"shopSaleResult\":{\"summary\": {\"score\":90.0, \"total\": 6, \"abnormal\": 1}, \"result\":[\n"
            + "        {\"content\":\"单均消费10\", \"result\":\"0异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费11\", \"result\":\"0异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费12\", \"result\":\"0异常\",\"score\":20,\"level\":1}\n" + "        ]},\n"
            + "    \"shopInoutResult\":{\"summary\": {\"score\":90.0, \"total\": 2, \"abnormal\": 0}, \"result\":[\n"
            + "        {\"content\":\"单均消费13\", \"result\":\"15个门店异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费14\", \"result\":\"3个门店异常\",\"score\":20,\"level\":1},\n"
            + "        {\"content\":\"单均消费15\", \"result\":\"0异常\",\"score\":20,\"level\":1}\n" + "        ]}\n" + "    }";

        System.out.println(json);
    }


    public static String processTemplate(String template, Map<String, Object> params) {
        StringBuffer sb = new StringBuffer();
        String pattern = "\\$\\{\\w+\\}";
        Matcher m = Pattern.compile(pattern).matcher(template);
        while (m.find()) {
            String param = m.group();
            Object value = params.get(param.substring(2, param.length() - 1));
            if(value==null) {
                log.warn("param is not exist. {}", param);
                return null;
            }
//            m.appendReplacement(sb, value == null ? "" : value.toString());
            m.appendReplacement(sb, value.toString());
        }
        m.appendTail(sb);
        return sb.toString();
    }
    // 仅仅修改参数为JSONObject
    public static String processTemplate(String template, JSONObject params) {
        StringBuffer sb = new StringBuffer();
        String pattern = "\\$\\{\\w+\\}";
        Matcher m = Pattern.compile(pattern).matcher(template);
        while (m.find()) {
            String param = m.group();
            Object value = params.get(param.substring(2, param.length() - 1));
            if(value==null) {
                log.warn("param is not exist. {}", param);
                return null;
            }
            //            m.appendReplacement(sb, value == null ? "" : value.toString());
            m.appendReplacement(sb, value.toString());
        }
        m.appendTail(sb);
        return sb.toString();
    }



    /*
    SELECT  YEARWEEK(reportDate) AS dt,SUM(foodAmount) AS metrics
    FROM tbl_saas_order_master
    WHERE reportDate >= ${dt1} AND reportDate <= ${dt2} AND groupID = ${groupID}
    GROUP BY dt
    */
    public static void main(String[] args) {
//        String str = "2024-03-01^147485276, 2024-03-29^105741147, 2024-03-09^115193303, 2024-04-01^105526644, 2024-03-19^123597254, 2024-03-20^122842927, 2024-03-26^110655303, 2024-03-06^140575959, 2024-03-14^135256279, 2024-03-25^110198526, 2024-03-12^136642618, 2024-03-02^134671821, 2024-03-10^115687251, 2024-03-04^143847329, 2024-03-18^126295905, 2024-03-07^132395708, 2024-03-31^93203491, 2024-03-23^100720047, 2024-03-27^109960719, 2024-03-22^111193309, 2024-03-13^137682658, 2024-03-16^112620437, 2024-03-08^116921591, 2024-03-17^110821272, 2024-03-03^128484557, 2024-03-11^131251172, 2024-03-21^117256645, 2024-03-30^96156626, 2024-03-28^110138625, 2024-03-15^125659857, 2024-03-24^97362515, 2024-03-05^143985682";
//        String str2 = sortSeries(str);
//        log.info("{}", str2);
//        String t = "2021-11-15 00:00:00";
//        log.info("[{}]", t.substring(0,10));
//        String sql = "    SELECT  YEARWEEK(reportDate) AS dt,SUM(foodAmount) AS metrics\n"
//            + "    FROM tbl_saas_order_master\n"
//            + "    WHERE reportDate >= ${dt1} AND reportDate <= ${dt2} AND groupID = ${groupID}\n"
//            + "    GROUP BY dt ";
//        Map<String, Object> params = new HashMap<>(3);
//        params.put("dt1",20190101L);
//        params.put("dt2",20200101L);
//        params.put("groupID",1155);
//        String instSQL = processTemplate(sql, params);
//        log.info("{}", instSQL);
    }

}
